package com.wcj.graph;

import java.util.ArrayList;

public class 邻接表表示 {
}

class Graph2{
    private ArrayList<Integer> vertices;
    private ListNode[] edges;
    private int vertexCount = 0;

    public Graph2(int vertexCount) {
        this.vertexCount = vertexCount;
        vertices = new ArrayList<>();
        edges = new ListNode[vertexCount];
        for (int i = 0; i < vertexCount; i++){
            vertices.add(i);
            edges[i] = new ListNode();
        }
    }

    public void addEdge(int source,int destination){
        int i = vertices.indexOf(source);
        int j = vertices.indexOf(destination);
        if (i != -1 || j != -1){
            edges[i].insertAtBeginning(destination);
            edges[j].insertAtBeginning(source);
        }
    }
}


class ListNode{
    int val;
    ListNode next;

    public void insertAtBeginning(int destination) {
        ListNode listNode = new ListNode();
        listNode.val = destination;
        listNode.next = this.next;
        this.next = listNode;
    }
}